package com.yycx.module.bus.provider.handler.invoice;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.module.bus.client.entity.*;
import org.springframework.stereotype.Component;

/**
 * 待开票税金明细列表扩展器
 *
 * @author zyf
 */
@Component("noInvoiceDetailsPageListHandler")
public class NoInvoiceDetailsPageListHandler implements PageInterceptor<BusOrderDetails> {


    @Override
    public void prepare(CriteriaQuery<BusOrderDetails> cq, PageParams pageParams, EntityMap params) {
        cq.clear();
        Long companyId=OpenHelper.getCompanyId();
        String beginDate = params.get("beginDate");
        String endDate = params.get("endDate");
        cq.addSelect(BusOrder.class, "orderId","orderNo","sendDate","supplierId");
        cq.addSelect(BusOrderDetails.class,"orderDetailsId","salePrice", "saleNum","supplierRetable","supplierRetable2","returnDate","returnAmount","saleAmount","kaipiaoId","kaipiaoId2");
        cq.addSelect(BusSupplier.class,"supplierName");
        cq.addSelect(BusDept.class,"deptName");
        cq.addSelect(BusSaleman.class,"saleManName");
        cq.addSelect(BusCustom.class,"customName");
        cq.addSelect(BusDrugs.class, "drugsName", "drugsSpec");
        //筛选条件
        cq.like(BusOrder.class, "orderNo");
        cq.eq(BusSupplier.class,"supplierId");
        //已审批订单
        cq.eq(BusOrder.class,"orderState",CommonConstants.INT_2);
        cq.eq(true, "bod.companyId", OpenHelper.getCompanyId());
        if(companyId.equals(4L)){
            //允许未回款
            cq.and(e->e.isNotNull("bod.dianfuId").or().in(true,"ord.supplierBondType",5,0));
        }else{
            //已回款或者全额垫付
            cq.and(e->e.isNotNull("bod.dianfuId").eq(true,"ord.supplierBondType",5).or().eq(true,"bod.customReturnState",CommonConstants.INT_2));
        }

        //未开票
        cq.and(e ->e.and(i->i.isNull("bod.kaipiaoId").ne("bod.supplierRetable",0)).or(y->y.isNull("bod.kaipiaoId2").ne("bod.supplierRetable2",0)));
        if (FlymeUtils.isNotEmpty(beginDate)) {
            cq.ge(BusOrder.class, "sendDate", beginDate);
        }
        if (FlymeUtils.isNotEmpty(endDate)) {
            cq.le(BusOrder.class, "sendDate", endDate);
        }
        cq.createJoin(BusDept.class);
        cq.createJoin(BusSaleman.class);
    }
}
